package Meituan;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author: leiwenkang
 * @date: 2020/5/25
 * @time: 14:22
 */
public class FileParser {
    public static void main(String[] args) {
        String pathName = "/Users/leiwenkang/Downloads/project/src/剑指offer-study/src/main/resources/quizzes_repository_platform_backup_origin";
        parseByteFile(pathName);
    }

    public static void parseByteFile(String pathName) {

        FileReader input = null;
        try {
            File filename = new File(pathName);
            InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
            BufferedReader br = new BufferedReader(reader);
            String line;
            Map<Integer, Integer> map = new LinkedHashMap<>();
            map.put(1, 21000);
            map.put(2, 22000);
            map.put(3, 23000);
            List<ContentInfo> contents = Lists.newArrayList();
            while(StringUtils.isNotBlank(line = br.readLine())) {
                List<String> list = Splitter.on(" ").splitToList(line);
                if (list.size() != 6) {
                    System.out.println("错误的行，" + line);
                    continue;
                }
                Integer grade = getGrade(list.get(1));
                if (grade == null) {
                    throw new RuntimeException("grade is error, id :");
                } else {
                    Integer id = map.get(grade) + 1;
                    map.put(grade, id);
                    ContentInfo contentInfo = new ContentInfo();
                    String content = "{id:" + id + ",type:\"backup\",grade:" + grade + ",type:\"backup\",content:\"" + list.get(0) + "\",options:[\"" + list.get(2) + "\",\"" + list.get(3) + "\",\"" + list.get(4) +  "\",\"" + list.get(5) + "\"],handler:\"FixHandler\",optionsConfig:{count:4,correctType:1,correctPos:0}}";
                    contentInfo.setContent(content);
                    contentInfo.setId(id);
                    contents.add(contentInfo);
                }
            }

            Collections.sort(contents, new Comparator<ContentInfo>() {
                @Override
                public int compare(ContentInfo o1, ContentInfo o2) {
                    return o1.id - o2.id;
                }
            });

            for (int i = 0; i < contents.size(); i++) {
                System.out.println(contents.get(i).getContent());
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                input.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static Integer getGrade(String gradeInfo) {
        if (gradeInfo.contains("一")) {
            return 1;
        } else if (gradeInfo.contains("二")) {
            return 2;
        } else if (gradeInfo.contains("三")) {
            return 3;
        }

        return null;
    }


}